package ua.pp.shell.wka;

import java.util.Date;
import ua.pp.shell.wka.DBAdapter.Types.Vocabulary;
import ua.pp.shell.wka.DBAdapter.Types.Word;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AEWordActivity extends Activity{
	private  DBAdapter dbAdapter = null;
	private Word word = null;
	private Vocabulary vocabulary = null;
	
	public String GetDate(long ticks) {
		Date date = null;
		if (ticks != 0) {
			date = new Date(ticks);
		} else {
			return getString(R.string.not_set);
		}
		return date.toLocaleString();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Settings settings = new Settings(getApplicationContext());
        dbAdapter = new DBAdapter(this);
        Intent intent = getIntent();
        int word_id = intent.getIntExtra("id", -1);
        if (word_id > 0) {
        	word = dbAdapter.GetWord(word_id);
        } else {
        	word = new Word();
            word.id_vocabulary = intent.getIntExtra("id_vocabulary", settings.GetCurrentVocabulary());
             if (word.id_vocabulary < 0) {
             	finish();
             }
        }
        vocabulary = dbAdapter.GetVocabulary(word.id_vocabulary);
        setContentView(R.layout.aeword_activity);
        ((TextView)findViewById(R.id.vocabulary_title)).setText(getString(R.string.vocabulary)+": "+vocabulary.title);
        ((EditText)findViewById(R.id.word)).setText(word.word);
        ((EditText)findViewById(R.id.translation)).setText(word.translation);
        ((TextView)findViewById(R.id.t_created)).setText(getString(R.string.t_created)+": "+ GetDate(word.t_created));
        ((TextView)findViewById(R.id.t_last_seen)).setText(getString(R.string.t_last_seen)+": "+ GetDate(word.t_last_seen));
        ((TextView)findViewById(R.id.t_last_error)).setText(getString(R.string.t_last_error)+": "+ GetDate(word.t_last_error));
        ((TextView)findViewById(R.id.t_last_success)).setText(getString(R.string.t_last_success)+": "+ GetDate(word.t_last_success));
        ((TextView)findViewById(R.id.c_shown)).setText(getString(R.string.c_shown)+": "+ Integer.toString(word.c_shown));
        ((TextView)findViewById(R.id.c_success)).setText(getString(R.string.c_success)+": "+ Integer.toString(word.c_success));
        ((TextView)findViewById(R.id.c_error)).setText(getString(R.string.c_error)+": "+ Integer.toString(word.c_error));
        Button translate = (Button) findViewById(R.id.translate);
        translate.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				try {
					EditText word = (EditText)findViewById(R.id.word);
					EditText translation = (EditText)findViewById(R.id.translation);
					String word_text = word.getText().toString().trim();
					if (word_text == "") {
						Toast.makeText(getApplicationContext(), R.string.title_can_not_be_empty, 2000).show();
	            		return;
					}
					String translation_text = Translator.translate(word_text, Translator.GetLanguage(vocabulary.language_from) ,  Translator.GetLanguage(vocabulary.language_to));
					translation.setText(translation_text);
					Toast.makeText(getApplicationContext(), translation_text, 2000).show();
				} catch (Exception e) {
					e.printStackTrace();
				}				
			}
        	
			
        });
        Button ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				word.word = ((EditText)findViewById(R.id.word)).getText().toString();
            	word.translation = ((EditText)findViewById(R.id.translation)).getText().toString();
            	if (word.word.trim().length() == 0) {
            		Toast.makeText(getApplicationContext(), R.string.title_can_not_be_empty, 2000).show();
            		return;
            	}
            	boolean add_new = false;
            	if (word.id > -1) {
            		dbAdapter.UpdateWord(word);
            		add_new = false;
            	} else {
            		word.id = dbAdapter.InsertWord(word);
            		add_new = true;
            	}
            	Intent intent = new Intent();
                intent.putExtra("id", word.id);
                intent.putExtra("add_new", add_new);
                setResult(RESULT_OK, intent);
                finish();  
			}
			
        });
        Button cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setResult(RESULT_CANCELED, null);
            	finish();
			}
        });
    }
}
